package com.general.framework.core.schema;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.general.framework.core.exception.CodeEnum;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.exception.FeignResponseException;
import com.general.framework.core.exception.ICode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Objects;

/**
 * 统一响应体
 */
@Data
@ApiModel("统一响应体")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> {

    @ApiModelProperty("响应码")
    private Integer code;

    @ApiModelProperty("相应描述")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    @ApiModelProperty("错误详情")
    private String errorMsg;

    public static <T> Response<T> error(final ICode code) {
        return new Response(code.getCode(), code.getMessage(), null);
    }

    public static <T> Response<T> error(final ICode code, final String errorMsg) {
        return new Response(code.getCode(), code.getMessage(), errorMsg);
    }

    public static <T> Response<T> error(final String msg) {
        return new Response(CodeEnum.SYSTEM_ERROR.getCode(), msg, null);
    }

    public static <T> Response<T> error(final Integer code, final String msg) {
        return new Response(code, msg, null);
    }

    public static <T> Response<T> success(final T data) {
        return new Response(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Response<T> success() {
        return new Response(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Response<T> successWithMsg(String msg) {
        return new Response(CodeEnum.SUCCESS.getCode(), msg, null);
    }

    public Response() {

    }

    /**
     * 获取成功后的接口
     *
     * @param response
     * @param <T>
     * @return
     */
    public static <T> T getSuccessData(Response<T> response) {
        if (response == null) {
            throw Ex.business(CodeEnum.SYSTEM_ERROR.getCode(), CodeEnum.SUCCESS.getMessage());
        }
        final boolean isSuccess = Response.isSuccess(response);
        if (isSuccess) {
            return response.getData();
        }
        throw new FeignResponseException(response.getCode(), response.getMsg());
    }

    /**
     * 是否成功
     *
     * @param response
     * @return
     */
    public static boolean isSuccess(Response response) {
        if (response == null) {
            return false;
        }
        if (Objects.equals(CodeEnum.SUCCESS.getCode(), response.getCode())) {
            return true;
        }
        return false;
    }

    private Response(final Integer code, final String msg, final T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Response(final Integer code, final String msg, final String errorMsg) {
        this.code = code;
        this.msg = msg;
        this.errorMsg = errorMsg;
    }

}
